//
//  Extension+STButton.swift
//  STVVeibo
//
//  Created by leergou on 16/7/24.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

extension UIButton{
    
    // 便利构造函数- 带标题和点击事件,不带图片
    convenience init(title: String?, titleColro: UIColor? = UIColor.blackColor(), font: CGFloat? = 15, target: AnyObject?, action: Selector?) {
        self.init()
        
        // 根据传进来的参数判定 是设置 标题 还是 图片
        if let tit = title {
            setTitle(tit, forState: UIControlState.Normal)
        }
        
        if let titColor = titleColro {
            setTitleColor(titColor, forState: UIControlState.Normal)
        }
        
        if let titleFont = font {
            titleLabel?.font = UIFont.systemFontOfSize(titleFont)
        }
        
        // 给 button 添加点击事件
        if let tar = target, act = action {
            addTarget(tar, action: act, forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        //        sizeToFit()
    }
    
    // 带图片和点击事件,不带标题
    // 便利构造函数
    convenience init(backgNorImg: String?, backgSelImg: String?, norImg:String?, selImg: String?,target: AnyObject?, action: Selector?) {
        self.init()
        
        if let backgNor = backgNorImg {
            setBackgroundImage(UIImage(named: backgNor), forState: UIControlState.Normal)
        }
        
        if let backgSel = backgSelImg {
            setBackgroundImage(UIImage(named: backgSel), forState: UIControlState.Selected)
        }
        
        if let norI = norImg {
            setImage(UIImage(named: norI), forState: UIControlState.Normal)
        }
        
        if let selI = selImg {
            setImage(UIImage(named: selI), forState: UIControlState.Selected)
        }
        
        // 给 button 添加点击事件
        if let tar = target, act = action {
            addTarget(tar, action: act, forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        //        sizeToFit()
    }
    // 
    convenience init(backgNorImg: String?, backghighlighted: String?, norImg:String?, highlightedImg: String?,target: AnyObject?, action: Selector?) {
        self.init()
        
        if let backgNor = backgNorImg {
            setBackgroundImage(UIImage(named: backgNor), forState: UIControlState.Normal)
        }
        
        if let backghigh = backghighlighted {
            setBackgroundImage(UIImage(named: backghigh), forState: UIControlState.Highlighted)
        }
        
        if let norI = norImg {
            setImage(UIImage(named: norI), forState: UIControlState.Normal)
        }
        
        if let highImg = highlightedImg {
            setImage(UIImage(named: highImg), forState: UIControlState.Highlighted)
        }
        
        // 给 button 添加点击事件
        if let tar = target, act = action {
            addTarget(tar, action: act, forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        //        sizeToFit()
    }

    
    
    // 带一个normal图片和点击事件,标题
    // 便利构造函数
    convenience init(title: String?, imageName:String? ,target: AnyObject?, action: Selector?) {
        self.init()
    
        // 根据传进来的参数判定 是设置 标题 还是 图片
        if let tit = title {
            setTitle(tit, forState: UIControlState.Normal)
        }
        
        if let norImage  = imageName {
            setImage(UIImage(named: norImage), forState: UIControlState.Normal)
        }
        
        // 给 button 添加点击事件
        if let tar = target, act = action {
            addTarget(tar, action: act, forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        //        sizeToFit()
    }
    
    
    
    
}
